#include "./../inc/lcd.h"
#include "./../inc/touch.h"
#include "./../inc/Chess/Board.h"
#include "./../inc/Chess/Pieces.h"
#include "./../inc/face.h"
#include "./../inc/Chess/AI.h"
#include "./../inc/music.h"  
#include <iostream>
using namespace std;




int main(void)
{
    // 初始化触摸屏--》单实例
    Touch *ts = Touch::instance();
    // 初始化液晶屏--》单实例
    Lcd *lcd = Lcd::instance();

    Face face;
    face.show_home_screen();
    int game_result = 0;
    
    LocalMusicPlayer musicPlayer("/root/music");
    musicPlayer.playRandom();

    while (1)
    {
        int start_result = face.wait_for_start();

        if (start_result == 1) // 人人对战
        {
            cout << "开始人人对战" << endl;

            Board board;
            board.init_piece(lcd);
            board.show_piece();

            Point p1(100, 200);
            Point p2(100, 200);

            bool game_running = true;
            while (game_running)
            {
                ts->wait(p1);
                int x = p1.x();
                int y = p1.y();
                cout << "x:" << x << " y:" << y << endl;
                cout << "row:" << x / 82 << " col:" << y / 52 << endl;
                // 获取点击的棋子是否为空
                Piece *selectedPiece = board.get_piece_at(x, y);
                if (selectedPiece == nullptr)
                    continue;

                ts->wait(p2);
                int to_x = p2.x();
                int to_y = p2.y();

                int game_result = board.move(x, y, to_x, to_y);

                if (game_result == 1 || game_result == 2)
                {
                    face.show_game_over_screen();

                    int choice = face.wait_for_start();
                    if (choice == 2)
                    {
                        // 重新开始
                        board.replay();
                    }
                    else if (choice == 3)
                    {
                        // 返回首页
                        face.show_home_screen();
                        game_running = false;
                    }
                }
            }
        }
        else if (start_result == 2) // 人机对战
        {
            cout << "开始人机对战" << endl;

            Board board;
            board.init_piece(lcd);
            board.show_piece();

            // 创建AI对象（控制黑方）
            AI ai(&board, false);

            Point p1(100, 200);
            Point p2(100, 200);

            bool game_running = true;
            while (game_running)
            {
                // 检查当前是否是玩家回合（红方）
                if (board.get_current_role() == 0)
                {
                    // 玩家回合
                    ts->wait(p1);
                    int x = p1.x();
                    int y = p1.y();
                    cout << "玩家 x:" << x << " y:" << y << endl;
                    cout << "row:" << x / 82 << " col:" << y / 52 << endl;

                    // 获取点击的棋子是否为空
                    Piece *selectedPiece = board.get_piece_at(x, y);
                    if (selectedPiece == nullptr)
                        continue;

                    ts->wait(p2);
                    int to_x = p2.x();
                    int to_y = p2.y();

                    game_result = board.move(x, y, to_x, to_y);
                }
                else
                {
                    // AI回合（黑方）
                    board.showExampleText("AI思考中...");

                    // 调用AI执行移动
                    bool ai_move_result = ai.makeMove();

                    if (!ai_move_result)
                    {
                        board.showExampleText("AI无法移动!");
                    }
                    else
                    {
                        board.showExampleText("AI回合完成");
                    }

                    // 检查游戏是否结束
                    game_result = board.check_win_condition();

                 
                }
                // 判断游戏是否结束
                if (game_result == 1 || game_result == 2)
                {
                    face.show_game_over_screen();

                    int choice = face.wait_for_start();
                    if (choice == 2)
                    {
                        // 重新开始
                        board.replay();
                    }
                    else if (choice == 3)
                    {
                        // 返回首页
                        face.show_home_screen();
                        game_running = false;
                    }
                }
                usleep(500000); 
            }
        }
    }
}
                                                                         